﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using KZ.Utilities;
using KZ.Express.H.ENT;

namespace KZ.Express.H.BLL
{
    public class OrderBLL : BaseBLL, IBaseBLL
    {
        public void SaveCS(List<DataRow> changedRows)
        {
            try
            {
                _dal.BeginTransaction();

                foreach (DataRow row in changedRows)
                {
                    CSOrderENT order = row.ToEntity<CSOrderENT>();
                    order.CreatedBy = UserENT._USERID;
                    order.UpdatedBy = UserENT._USERID;

                    order.ValidateData();

                    if (order.OrderID <= 0)
                        _dal.Insert(order);
                    else
                        _dal.Update(order);
                }

                _dal.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }

        public void SaveSale(List<DataRow> changedRows)
        {
            try
            {
                _dal.BeginTransaction();

                foreach (DataRow row in changedRows)
                {
                    SaleOrderENT order = row.ToEntity<SaleOrderENT>();
                    order.CreatedBy = UserENT._USERID;
                    order.UpdatedBy = UserENT._USERID;

                    order.ValidateData();

                    _dal.Update(order);
                }

                _dal.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }

        public void SaveAccount(List<DataRow> changedRows)
        {
            try
            {
                _dal.BeginTransaction();

                foreach (DataRow row in changedRows)
                {
                    AccountOrderENT order = row.ToEntity<AccountOrderENT>();
                    order.CreatedBy = UserENT._USERID;
                    order.UpdatedBy = UserENT._USERID;

                    order.ValidateData();

                    if (order.OrderID <= 0)
                        _dal.Insert(order);
                    else
                        _dal.Update(order);
                }

                _dal.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }

        public void SaveAdmin(List<DataRow> changedRows)
        {
            try
            {
                _dal.BeginTransaction();

                foreach (DataRow row in changedRows)
                {
                    AdminOrderENT order = row.ToEntity<AdminOrderENT>();
                    order.CreatedBy = UserENT._USERID;
                    order.UpdatedBy = UserENT._USERID;

                    order.ValidateData();

                    if (order.OrderID <= 0)
                        _dal.Insert(order);
                    else
                        _dal.Update(order);
                }

                _dal.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }

        public void UpdateStandardPrice(DateTime from, DateTime to, int carrierID, int supplierID)
        {
            try
            {
                _dal.BeginTransaction();

                SearchCriteria search = new SearchCriteria();
                search.Add("From", from);
                search.Add("To", to);
                search.Add("CarrierID", carrierID);
                search.Add("SupplierID", supplierID);

                DataTable dt = _dal.GetDataTable<OrderENT>(search);
                foreach (DataRow row in dt.Rows)
                {
                    search.Clear();
                    search.Add("OrderID", row["OrderID"]);
                    search.Add("OrderNumber", row["OrderNumber"]);
                    search.Add("OrderDate", row["OrderDate"]);
                    search.Add("Type", row["Type"]);
                    search.Add("SupplierID", supplierID);
                    search.Add("CarrierID", carrierID);
                    search.Add("CountryID", row["CountryID"]);
                    search.Add("Weight", row["Weight"]);
                    search.Add("UserID", UserENT._USERID);

                    _dal.ExecuteNonQuery(OrderENT.UpdateStandardPriceProcedure, search);
                }

                _dal.CommitTransaction();
            }
            catch (DataAccessException ex)
            {
                _dal.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}
